package com.yonyou.pmclouds.billview.plugin;

import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.BeanHelper;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.basecom.util.StorageFactory;
import com.yonyou.pmclouds.businessobject.util.BusinessObjectConsts;
import com.yonyou.pmclouds.businessobject.util.PrintPreviewObj2Html;
import com.yonyou.pmclouds.comprehensiveacceptance.entity.ComprehensiveAcceptanceVO;
import com.yonyou.pmclouds.enterprise.entity.EnterpriseInfo;
import com.yonyou.pmclouds.inspectitem.entity.InspectItemVO;
import com.yonyou.pmclouds.outter.enterprise.EnterpriseInfoQuery;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Component
public class ProjectCheckViewPlugin implements IBillViewCustomPlugin {

    @Autowired
    private UserInfoQuery userInfoQuery;
    @Autowired
    private StorageFactory storageFactory;
    @Autowired
    private EnterpriseInfoQuery enterpriseInfoQuery;
    @Value("${print.perLineNum}")
    private int perLineNum;

    @Override
    public Object[] fillListBuziField(Object[] objects) throws BusinessException {
        Set<String> constructionUnitPks = new HashSet<>();

        ComprehensiveAcceptanceVO[] comprehensiveAcceptanceVOs = new ComprehensiveAcceptanceVO[objects.length];

        for (int i = 0; i < objects.length ; i++){
            comprehensiveAcceptanceVOs[i] = (ComprehensiveAcceptanceVO)objects[i];
        }

        // 取到所有施工单位的pk,获取施工单位信息
        for (ComprehensiveAcceptanceVO comprehensiveAcceptanceVO : comprehensiveAcceptanceVOs) {
            constructionUnitPks.add(comprehensiveAcceptanceVO.getPkConstructionunit());
        }
        Map<String, EnterpriseInfo> enterpriseInfoMap = enterpriseInfoQuery
                .queryEnterpriseByIds(constructionUnitPks.toArray(new String[0]));
        for (ComprehensiveAcceptanceVO comprehensiveAcceptanceVO : comprehensiveAcceptanceVOs) {
            EnterpriseInfo enterpriseInfo = enterpriseInfoMap.get(comprehensiveAcceptanceVO.getPkConstructionunit());
            comprehensiveAcceptanceVO.setConstructionName(enterpriseInfo==null?"":enterpriseInfo.getName());
        }

        userInfoQuery.tranUserInfoField(comprehensiveAcceptanceVOs,new String[]{ComprehensiveAcceptanceVO.CREATOR},new String[]{ComprehensiveAcceptanceVO.CREATOR_NAME});
        return comprehensiveAcceptanceVOs;

    }

    @Override
    public Map<String, Object[]> fillDetailBuziField(SuperVO superVO, HttpServletRequest request) throws BusinessException {

        InspectItemVO[] inspectitems =  (InspectItemVO[]) BeanHelper.getProperty(superVO,"inspectitems");

        userInfoQuery.tranUserInfoField(new Object[]{superVO},new String[]{CommonFieldConst.CREATOR, CommonFieldConst.MANAGER},new String[]{CommonFieldConst.CREATOR_NAME, CommonFieldConst.MANAGER_NAME});


            String pk_constructionunit = BeanHelper.getProperty(superVO,"pkConstructionunit")==null?"":(String)BeanHelper.getProperty(superVO,"pkConstructionunit");

            Map<String, EnterpriseInfo> enterpriseInfoMap = enterpriseInfoQuery
                    .queryEnterpriseByIds(new String[]{pk_constructionunit});

        if(MapUtils.isNotEmpty(enterpriseInfoMap)){
            EnterpriseInfo enterpriseInfo = enterpriseInfoMap.get(pk_constructionunit);
            BeanHelper.setProperty(superVO,"constructionName",enterpriseInfo==null?"":enterpriseInfo.getName());
        }

        PrintPreviewObj2Html obj2Html = new PrintPreviewObj2Html(PrintPreviewObj2Html.BASE_PREFIX, PrintPreviewObj2Html.IMG, perLineNum);

        if (inspectitems != null) {
            for (InspectItemVO inspectItemVO : inspectitems) {
                if (inspectItemVO.getPictures() != null) {
                    String picHtml = obj2Html.urls2Html(storageFactory.getStorageUtil().signUrlForMultiPictures(inspectItemVO.getPictures(), null, request));
                    inspectItemVO.setPicture_address(picHtml);
                }
            }
            BeanHelper.setProperty(superVO,"inspectitems",inspectitems);
        }

        Map<String,Object[]> resultMap = new HashMap<String,Object[]>();

        resultMap.put(BusinessObjectConsts.PM_COMPREHENSIVE_ACCEPTANCE, new Object[]{superVO});
        resultMap.put("pm_check_item", inspectitems);

        return resultMap;
    }

    @Override
    public String getBillType() {
        return BillTypeConst.COMPREHENSIVE_ACCEPTANCE_BILLTYPE;
    }
}
